<template>
    <el-card style="margin-bottom:10px">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="角色名称：">
                <el-input v-model="formInline.name" placeholder="请输入角色名称" clearable />
            </el-form-item>
            <el-form-item label="角色标识：">
                <el-input v-model="formInline.identity" placeholder="请输入角色标识" clearable />
            </el-form-item>
            <el-form-item label="状态：">
                <el-select v-model="formInline.status" placeholder="请选择状态" clearable>
                    <el-option label="启用" :value="true" />
                    <el-option label="关闭" :value="false" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onQuery">搜索</el-button>
                <el-button @click="onReset">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
    <el-card>
        <div style="display:flex;justify-content: space-between;">
            <h3>角色列表</h3>
            <el-button type="primary" @click="onAdd">新增角色</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%;margin-bottom: 10px;">
            <el-table-column fixed prop="id" label="角色编号" width="180" />
            <el-table-column prop="name" label="角色名称" width="100" />
            <el-table-column prop="identity" label="角色标识" width="120" />
            <el-table-column prop="status" label="状态" width="100">
                <template #default="scope">
                    <el-switch v-model="scope.row.status" disabled />
                </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" show-overflow-tooltip />
            <el-table-column prop="createtime" label="创建时间" width="180" />
            <el-table-column fixed="right" label="操作" width="100">
                <template #default="scope">
                    <el-button link type="primary" size="small" @click="onEdit(scope.row)">
                        修改
                    </el-button>
                    <el-popconfirm title="是否确认删除?" @confirm="onConfirm(scope.row)">
                        <template #reference>
                            <el-button link type="primary" size="small">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="->,total,prev, pager, next" :total="total" @current-change="handleChange" />
    </el-card>
    <el-dialog v-model="dialogFormVisible" :title="flag ? '新增角色' : '修改角色'" width="500">
        <el-form :model="form">
            <el-form-item label="角色名称" label-width="80px">
                <el-input v-model="form.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="角色标识" label-width="80px">
                <el-input v-model="form.identity" autocomplete="off" />
            </el-form-item>
            <el-form-item label="备注" label-width="80px">
                <el-input v-model="form.remark" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="onSure">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script>
import request from '@/utils/request.js'
import { ElMessage } from 'element-plus'
export default {
    data() {
        return {
            formInline: {
                name: '', // 角色名称
                identity: '', // 角色标识
                status: '',
                pagination: 1,
                pageNum: 10
            },
            tableData: [],
            total: null,
            dialogFormVisible: false,
            form: {
                name: '',
                identity: '',
                remark: ""
            },
            flag: true, // 判断当前是新增还是修改
            rowId: '' // 用来存储修改时的id
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            request.get('/list', {
                params: this.formInline
            }).then(res => {
                if (res.data.code == 200) {
                    console.log(res, 'res')
                    this.tableData = res.data.data
                    this.total = res.data.total
                }
            })
        },
        handleChange(val) {
            console.log(val)
            this.formInline.pagination = val
            this.getList()
        },
        onQuery() {
            this.getList()
        },
        onReset() {
            // 清空formInline的值、 将formInline的值设置为初始值
            this.formInline = {
                name: '', // 角色名称
                identity: '', // 角色标识
                status: '',
                pagination: 1,
                pageNum: 10
            }
            this.getList()
        },
        onAdd() {
            this.dialogFormVisible = true
            this.flag = true // flag为true时说明是新增
            this.form = {
                name: '',
                identity: '',
                reamrk: ''
            }
        },
        onSure() {
            // 请求新增的接口
            console.log(this.form, 'form')
            this.flag ?
                request.post('/add', this.form).then(res => {
                    console.log(res, 'res')
                    if (res.data.code == 200) {
                        // 提示新增成功
                        ElMessage({
                            message: '新增成功',
                            type: 'success',
                        })
                        // 关闭弹框
                        this.dialogFormVisible = false
                        // 拿到最新list / 刷新列表
                        this.getList()
                    }
                })
                :
                request.post('/edit', {
                    ...this.form,
                    id: this.rowId
                }).then(res => {
                    if (res.data.code == 200) {
                        // 提示修改成功
                        ElMessage({
                            message: '修改成功',
                            type: 'success',
                        })
                        // 关闭弹框
                        this.dialogFormVisible = false
                        // 拿到最新list / 刷新列表
                        this.getList()
                    }
                })
        },
        onEdit(row) {
            this.dialogFormVisible = true
            this.flag = false // flag为false时说明是修改
            this.rowId = row.id
            console.log(row, 'row')
            // 回显
            this.form = {
                name: row.name,
                identity: row.identity,
                remark: row.remark
            }
        },
        onConfirm(row) {
            console.log(row, 'onConfirm')
            request.post('/delete', {
                id: row.id
            }).then(res => {
                if (res.data.code == 200) {
                    // 提示删除成功
                    ElMessage({
                        message: '删除成功',
                        type: 'success',
                    })
                    // 刷新列表
                    // this.tableData = res.data.list
                    this.getList()
                }
            })
        }
    }
}

</script>

<style lang="scss" scoped>
.demo-form-inline .el-input {
    --el-input-width: 220px;
}

.demo-form-inline .el-select {
    --el-select-width: 220px;
}
</style>